<template>
  <div class="table">
    <el-button v-if="false" type="primary" size="small" @click="onClickRefresh()">
      刷新
    </el-button>
    <el-table :data="data" style="width: 100%" v-loading="loading">
      <el-table-column prop="id" label="序号" width="60px" align="center">
      </el-table-column>
      <el-table-column prop="name" label="服务器" min-width="250px" />
      <el-table-column prop="ip" label="IP" min-width="250px" />
      <el-table-column prop="port" label="端口" min-width="250px" />
    </el-table>
  </div>
</template>

<script lang="ts" setup>
import { onMounted, ref } from "vue";
import request from "../../../request";
import { ElMessage } from "element-plus";
const data = ref<any[]>([]);
const loading = ref<Boolean>(false);
const props = defineProps({
  serverLocation: String,
});
onMounted(() => {
  getData();
});
const onClickRefresh = () => {
  getData();
};
const getData = async () => {
  loading.value = true;
  let param = {
    serverLocation: props.serverLocation,
  };
  request
    .get("/gameServer/server/serversNew", param)
    .catch((error) => {
      console.log(error);
    })
    .then((result) => {
      const json = result.data;

      const resultData: any[] = [];
      if (Array.isArray(json)) {
        json.forEach((item, index) => {
          const resultDataItem = {
            id: item["id"] || "",
            name: item["name"] || "",
            ip: item["ip"] || "",
            port: item["port"] || "",
          };

          resultData.push(resultDataItem);
        });
      }

      data.value = resultData;
    })
    .catch((err) => {
      ElMessage.warning({
        message: "高级表格需要登录才能查阅",
      });
    });

  loading.value = false;
};
</script>